﻿Shader "AdditionalLighting"
{
    Properties
    {
        _BaseColor("Color",Color) = (1,1,1,1)
        _BaseMap("Texture",2D) = "white"{}
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalRenderPipeline"
            "LightMode"="UniversalForward"
            "RenderType"="Opaque"
        }
        LOD 100

        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
        
        #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS

        TEXTURE2D(_BaseMap);    SAMPLER(sampler_BaseMap);

        CBUFFER_START(UnityPerMaterial)
        float4 _BaseMap_ST;
        half4 _BaseColor;
        CBUFFER_END
        ENDHLSL
        
        pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            struct a2v
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
                float2 texcoord : TEXCOORD;
            };
            struct v2f
            {
                float4 positionCS : SV_POSITION;
                half3 normalWS : NORMAL;
                half3 positionWS : TEXCOORD0;
                half2 texcoord : TEXCOORD1;
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
                o.positionWS = TransformObjectToWorld(v.positionOS.xyz);
                o.normalWS =TransformObjectToWorldNormal(v.normalOS.xyz);
                o.texcoord = TRANSFORM_TEX(v.texcoord,_BaseMap);
                return o;
            }

            half4 frag(v2f i):SV_Target
            {
                Light light = GetMainLight();
                half3 normalWS = normalize(i.normalWS);
                half3 ambient = SampleSH(normalWS);

                half4 albedo = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.texcoord);
                half3 diffuse = _BaseColor.rgb* saturate(dot(normalWS, light.direction))* light.color;
                
                #ifdef _ADDITIONAL_LIGHTS
                    int pixelLightCount = GetAdditionalLightsCount();
                    for (int lightIndex = 0; lightIndex < pixelLightCount; ++lightIndex)
                    {
                        Light light = GetAdditionalLight(lightIndex, i.positionWS);
                        half3 attenuatedLightColor = light.color * light.distanceAttenuation; 
                        half NdotL = saturate(dot(normalWS, light.direction));
                        diffuse += attenuatedLightColor * NdotL;
                    }
                #endif
                albedo.rgb *= diffuse + ambient;
                return half4(albedo.rgb,1);
            }
            ENDHLSL
        }
    }
}
